<?php

class CBaseModel{
	
	public $_list_error;
	
	public function __construct(){
		foreach(get_object_vars($this) as $item => $opts){
			if(is_array($opts) && $opts['CModelField'] == true){
				$this->$item = new CModelField($item, $opts);
			}
		}
	}
	
	public function isValid($action=null, $param=null){
		//Validaciones definidas en el modelo
		$array = array();
		//Validaciones propias de la accion
		if($action!=null){
			$action = "validation$action";
			$actionArray = $this->$action($param);
			if(count($actionArray)>0){
				$array = $actionArray;
			}
		}
		$array = array_merge($array, CValidator::validateModel($this));
		
		if(count($array)>0){
			$this->_list_error = $array;
			return false;
		}
		$this->_list_error = null;
		return true;
	}
	
	function __set($name, $value){
		foreach(get_object_vars($this) as $item => $opts){
			if(get_class($opts)=='CModelField' && $opts->db['field']==$name){
				$this->$item->value = $value;
			}
		}
	}
	
}